const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
const sendToWormhole = require('stream-wormhole');
const awaitWriteStream = require('await-stream-ready').write;
const Pump = require('mz-modules/pump');
module.exports = class extends Controller {
    async upload_student_avatar() {
        // 获取文件流
        const stream = await this.ctx.getFileStream();
        // 防止上传空文件
        if (!stream.filename) {
            return;
        }
        let id = null;
        console.log('stream::::::::::', stream);
        // 定义文件名
        const filename = Date.now() + path.extname(stream.filename).toLocaleLowerCase();
        const fname = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(7) + path.extname(stream.filename).toLocaleLowerCase();
        console.log('filename:::::::::::::', fname);
        // 上传的目录，注意upload目录要存在
        const target = path.join('app/public/user_avatar', filename);
        // 创建写入流
        const writeStream = fs.createWriteStream(target);
        try {
            // 上传超过时长不会卡死
            await Pump(stream, writeStream);
            console.log('-----------获取表单中其它数据 start--------------');
            console.log(stream.fields);
            id = stream.fields.stu_id;
            console.log(id);
            console.log('-----------获取表单中其它数据 end--------------');
        } catch (err) {
            // 自定义方法
            console.log('Error', err);
        }
        // 写入对应角色的数据库中
        let picture = 'http://localhost:8890/public/user_avatar/' + filename;
        console.log('id2:::::',id);
        const result = await this.ctx.service.student.modify({
            picture,
            id
        });
        console.log('result::::::::',result);
        // // 自定义方法

        this.ctx.body = {
            code: 0,
            picture,
            // 表单的其它数据
            fields: stream.fields
        }
    }
    async upload_teacher_avatar() {
        // 获取文件流
        const stream = await this.ctx.getFileStream();
        // 防止上传空文件
        if (!stream.filename) {
            return;
        }
        let id = null;
        console.log('stream::::::::::', stream);
        // 定义文件名
        const filename = Date.now() + path.extname(stream.filename).toLocaleLowerCase();
        const fname = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(7) + path.extname(stream.filename).toLocaleLowerCase();
        console.log('filename:::::::::::::', fname);
        // 上传的目录，注意upload目录要存在
        const target = path.join('app/public/user_avatar', filename);
        // 创建写入流
        const writeStream = fs.createWriteStream(target);
        try {
            // 上传超过时长不会卡死
            await Pump(stream, writeStream);
            console.log('-----------获取表单中其它数据 start--------------');
            console.log(stream.fields);
            id = stream.fields.tea_id;
            console.log(id);
            console.log('-----------获取表单中其它数据 end--------------');
        } catch (err) {
            // 自定义方法
            console.log('Error', err);
        }
        // 写入对应角色的数据库中
        let picture = 'http://localhost:8890/public/user_avatar/' + filename;
        console.log('id2:::::',id);
        const result = await this.ctx.service.teacher.modify({
            picture,
            id
        });
        console.log('result::::::::',result);
        // // 自定义方法

        this.ctx.body = {
            code: 0,
            picture,
            // 表单的其它数据
            fields: stream.fields
        }
    }
}